<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
function logss($logname, $filename = '', $data)
{
    if ($filename) {
        $log_filename = $path = env('runtime_path') . 'log/' . $filename . '-' . date('Ym-d') . '.log';
    } else {
        $log_filename = $path = env('runtime_path') . 'log/' . date('Ym-d') . '.log';
    }
    if (is_array($data)) {
        $text = '【result】=' . var_export($data, true) . ';';
    } else {
        $text = '【 结 果 】：' . $data;
    }
    $contents = '[' . date('Y-m-d H:i:s', time()) . ']---[变量名:' . $logname . ']' . PHP_EOL . $text;
    //    $res = file_put_contents($log_filename, $contents . PHP_EOL, FILE_APPEND);
}

/**
 * 获取两个日期的所有天数.
 * @param $startDate 开始日期
 * @param $endDate 结束日期
 * @param $format string 格式
 */
function createDateRange($startDate = '', $endDate = '', string $format = 'Y-m-d'): array
{
    if (! $startDate || ! $endDate) {
        return [];
    }

    if ($startDate > $endDate) {
        return [];
    }

    $dateList = [];
    while ($startDate <= $endDate) {
        $dateList[] = $startDate;
        $startDate = date($format, strtotime("{$startDate} +1 day"));
    }

    return $dateList;
}

/**
 * 过滤媒体返回体.
 * @param mixed $data
 * @return array|mixed
 */
function filterData($data = [], ...$args)
{
    $res = [];
    if (count($args) && $data) {
        $res = $data;
        foreach ($args as $v) {
            $res = $res[$v] ?? [];
        }
    }
    return $res;
}

function getTotalMillisecond()
{
    $time = explode(' ', microtime());
    $time = $time[1] . ($time[0] * 1000);
    $time2 = explode('.', $time);
    return $time2[0];
}
function getMicrotimeFormat($time)
{
    $time = strval($time * 0.001);
    if (strstr($time, '.')) {
        [$usec, $sec] = explode('.', $time);
        $sec = str_pad($sec, 3, '0', STR_PAD_RIGHT);
    } else {
        $usec = $time;
        $sec = '000';
    }
    $date = date('m-d H:i:s.x', intval($usec));
    return str_replace('x', $sec, $date);
}

/**
 * 获取指定长度数组.
 * @param array $array 数组
 * @param int $num 长度
 * @return array
 */
function getNumArray(array $array = [], int $num = 0)
{
    if (! $array) {
        return $array;
    }
    if (count($array) >= $num) {
        $array = array_slice($array, 0, $num);
    } else {
        $array = array_fill(0, $num, $array);
        $result = array_reduce($array, function ($result, $value) {
            return array_merge($result, array_values($value));
        }, []);
        $array = array_slice($result, 0, $num);
    }
    return $array;
}

/**
 * 获取视频数组的下标(第几个).
 * @param int $allNum 总数
 * @param int $i 序号
 */
function getSubscript(int $allNum = 0, int $i = 1): int
{
    // 视频数量大于等于计划或组的第i个，正常返回下标
    if ($allNum >= $i) {
        return $i - 1;
    }
    // 取余，余数为0，返回最后一个ID下标，余数大于0，返回ID的下标
    $x = $i % $allNum;
    if ($x == 0) {
        $index = $allNum - 1;
    } else {
        $index = $x - 1;
    }
    // 返回下标
    return $index;
}

/**
 * 是否是预发环境.
 */
function isPreEnv(): bool
{
    return env('APP_ENV', 'prod') == 'pre';
}
